// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
cc.Class({
    extends: cc.Class,
    //------------------------------//
    /**
     * 
     */
    ctor() {
        this.m_cardSpriteList = []; //手牌内容
        this.m_outCardSpriteList = []; //出牌内容
        this.mQuick = false;
        this.mQuickType = 0;
        this.m_longitude = -1;
        this.m_latitude = -1;
        this.mPiao = -1;
        this.mIsFree = false; //是否已经使用
        this.m_boReady = false; //是否准备好
        this.m_btPlayOrd = 0; //玩的顺序
        this.m_dbid = 0;
        this.m_nSeadOrd = 0; //坐位号
        this.m_nCardNums = 0; //手上牌的数量
        this.m_nCards = []; //卡的具体数据        //最多108张
        this.m_nHandSordCard = null; //新的手牌的排列
        this.m_handSprite = null; //刚得到的手牌 
        this.m_nOutCount = 0; //出牌的具体数量
        this.m_nOutCards = []; //具体出牌内容
        this.m_boMingPai = false; //是否明牌 
        this.m_btOffline = false; //是否离线
        this.m_showName = ""; //显示的名字
        this.m_headUrl = ""; //头像路径
        this.m_headIdx = 0; //头像编号
        this.m_szAccount = ""; //账号
        this.m_ipArr = "";
        this.m_chrID = 0; //角色ID
        this.m_szNikeName = ""; //名字
        this.m_gold = 0; //金币
        this.m_btSex = 0; //性别
        this.m_point = 0; //分数
        this.m_silver = 0; //门票
        this.m_Level = 0;
        this.m_winCount = 0;
        this.m_loseCount = 0;
        this.m_adress = ""; //具体位置 
        this.mGameFace = null; //对应的头像 
        this.mActionFun = null; //动作库 如果有就执行相应的
    },


    initCardCheck() {

    },


    setGameAction(action) {
        if (this.mActionFun != null) {
            this.mActionFun.finish.call(this);
        }
        this.mActionFun = action;
        this.mActionFun.init.call(this);
    },

    getGameAction() {
        this.mActionFun;
    },

    getFunction(name) {
        if (this.mActionFun && this.mActionFun[name]) {
            return this.mActionFun[name];
        }
        return this[name];
    },


    setGameConfig(config) {

    },

    setGameFace(faceview) {
        this.mGameFace = faceview;
    },

    init(data) {
        this.m_chrID = data['id'];
        this.m_dbid = data['dbid'];
        if (!this.m_dbid) {
            this.m_dbid = this.m_chrID;
            this.m_silver = data['silver'];
        }
        this.m_szNikeName = data['name'];
        this.m_showName = this.m_szNikeName;
        this.m_point = data['point'];
        this.m_gold = data['gold'];
        this.m_headUrl = data['head'];
        this.m_btPlayOrd = data['playOrd'];
        this.m_boReady = data['ready'];
        this.m_ipArr = data['ip'];
        this.m_btSex = (data['sex'] & 0x03);
        this.m_btOffline = false;
        if (data.offline) {
            this.m_btOffline = data['offline'];
        }
        this.mQuick = false;
        this.mQuickType = 0;
        this.m_adress = "未知";
        this.m_longitude = -1;
        this.m_latitude = -1;
        this.mIsFree = false;
    },

    /**
     *  离开桌子
     */
    leaveTable() {
        this.mIsFree = true;
        if (this.mGameFace != null) {
            this.mGameFace.node.active = false;
            this.mGameFace = null;
        }
    },
    /**
     *   准备好了
     */
    doReady() {
        this.m_boReady = true;
    },

    setAdress(mapinfo) {
        if (mapinfo == null) {
            this.m_adress = "未知";
            this.m_longitude = -1;
            this.m_latitude = -1;
            return;
        }
        if (typeof mapinfo != 'string') {
            mapinfo = JSON.stringify(mapinfo);
        }
        var info = mapinfo.split("|"); //以逗号作为分隔字符串
        if (info.length == 3) {
            //判断位置
            this.m_adress = info[0];
            this.m_longitude = info[1] * 1;
            this.m_latitude = info[2] * 1;
        } else {
            this.m_adress = "未知";
            this.m_longitude = -1;
            this.m_latitude = -1;
        }
    },




    giveUpTurn(agree, time) {

    },

    setQuickState(isAgree, isShow) {
        if (this.mGameFace)
            this.mGameFace.setQuickState(isAgree);
        this.mQuick = isAgree;
    },

    setQuickType(type, isShow) {
        // if (this.mGameFace)
        //     this.mGameFace.setQuickType(type);
        this.mQuickType = type;
    },

    /**
     *  游戏开始
     */
    gameStart() {
        if (this.mActionFun && this.mActionFun.gameStart) {
            this.mActionFun.gameStart.call(this);
        }
    },

    /**
     *  游戏结束
     */
    gameOver() {
        if (this.mActionFun && this.mActionFun.gameOver) {
            this.mActionFun.gameOver.call(this);
        }
    },

    /**
     *  
     *  显示时间
     * @param {any} nTime
     */
    showTimer(nTime) {

    },

    reconnect(data) {

    },

    autoOutCard(isAuto) {

    },

    setOffLine(isOffline) {
        if (isOffline) {
            playEffect('disconnect');
        } else {
            playEffect('reconnect');
        }
    },

    /**
     *  隐藏时间
     */
    hideTimer() {

    },


    makeOutSprite() {
        if (this.mActionFun && this.mActionFun.makeOutSprite) {
            this.mActionFun.makeOutSprite.call(this);
        }
    },
    /**
     * 
     * 断线重连回来
     * @param {any} card
     */
    makeOutCard(lastOutCard) {
        if (this.mActionFun && this.mActionFun.makeOutCard) {
            this.mActionFun.makeOutCard.call(this, lastOutCard);
        }
    },

    removeOutCard(card) {
        if (this.mActionFun && this.mActionFun.removeOutCard) {
            this.mActionFun.removeOutCard.call(this, card);
        }
    },


    setGetCard() {

    },
    /**
     * 
     * 回合开始
     * @param {any} btFlag
     */
    turnOn(isReconnect) {
        if (isReconnect == null)
            isReconnect = false;
        this.showTimer(15);
        if (this.mActionFun && this.mActionFun.turnOff) {
            this.mActionFun.turnOn.call(this, isReconnect);
        }
    },



    outCard(card) {
        if (this.mActionFun && this.mActionFun.outCard) {
            this.mActionFun.outCard.call(this, card);
        }
    },

    outCards(cards) {
        if (this.mActionFun && this.mActionFun.outCards) {
            this.mActionFun.outCards.call(this, card);
        }
    },



    /**
     * 回合结束
     */
    turnOff() {
        if (this.mActionFun && this.mActionFun.turnOff) {
            this.mActionFun.turnOff.call(this);
        }
    },

    makeOutSprite() {

    },

    makeCardSprites(noSort) {
        if (this.mActionFun && this.mActionFun.makeCardSprites) {
            this.mActionFun.makeCardSprites.call(this, noSort);
        }
    },

    makeCPGSprites() {
        if (this.mActionFun && this.mActionFun.makeCPGSprites) {
            this.mActionFun.makeCPGSprites.call(this);
        }
    },



    makeOutSprite() {
        if (this.mActionFun && this.mActionFun.makeOutSprite) {
            this.mActionFun.makeOutSprite.call(this);
        }
    },

    /**
     *  清空出牌
     */
    clearOutCardSprites() {
        for (var index = 0; index < this.m_outCardSpriteList.length; index++) {
            var element = this.m_outCardSpriteList[index];
            element.node.active = false;
            element.node.removeFromParent();
        }
        this.m_outCardSpriteList = [];
    },


    playSound(sound) {
        if (this.m_btSex == 1) {
            if (soundType == 1) {
                playEffect('boy/' + 0 + '/' + sound);
            } else {
                playEffect('boy/' + PlayerManager.mEnterGameId + '/' + sound);
            }
        } else {
            if (soundType == 1) {
                playEffect('girl/' + 0 + '/' + sound);
            } else {
                playEffect('girl/' + PlayerManager.mEnterGameId + '/' + sound);
            }
        }
    }
});